<?php 
class UserController extends ApiController {
	
	/**
	 * @return array action filters
	 */
	public function filters() {
		$arr = array(
			'postOnly + register,updateProfile,logVisitor' // we only allow deletion via POST request
		);
		return array_merge(parent::filters(), $arr);
	}

	public function actionGetMetaData() {
		$c = new CDbCriteria();
		$c->compare('object', 'user');
		$c->order = 'field';
		$fields = FieldData::model()->findAll($c);
		$result = array();
		
		foreach($fields as $f) {
			$result[$f->field][] = array('id'=>$f->id,'label'=>$f->label);
		}
		$this->_sendSuccess($result);
	}
	
	public function actionCheckProfileID($value) {
		$u = new User();
		$u->inviter_profile_id = $value;
		$result = (int)$u->validate(array('inviter_profile_id'));
		$this->_sendSuccess($result);
	}
	
	public function actionCheckNickname($value) {
		$u = new User();
		$u->nickname = $value;
		$result = (int)$u->validate(array('nickname'));
		$this->_sendSuccess($result);
	}
	
	public function actionRegister() {
		$data = $_POST;
		$u = new User();
		$u->attributes = $data;
		$u->profile_id = $u->generateProfileID();
		$u->role_id = Role::ROLE_OPERATOR;
		$u->created_time = date('Y-m-d H:i:s');
		if (!$u->save()) {
			$this->_sendError($u->getErrors(), 400);
		}
		
		// add point for inviter user
		if ($inviter = $u->inviter) {
			$inviter->addPoint('invite_recv');
		}
		$u->saveImage();
		$u->createOpenfireAccount();
		$result = $u->id;
		$this->_sendSuccess($result);
	}
	
	public function actionUpdate($id) {
		$u = User::model()->findByPk($id);
		if (!$u) {
			$this->_sendError('User not exists', 400);
		}
		
		if (!$u->updatePublicInfo($_POST)) {
			$this->_sendError($u->getErrors(), 400);
		}
			
		$u->saveImage();
		$this->_sendSuccess('1');
	}
	
	public function actionGetInfo($id) {
		$u = User::model()->findByPk($id);
		if (!$u) {
			$this->_sendError('User not exists', 400);
		}
		$result = $u->getPublicInfo();
		$this->_sendSuccess($result);
	}
	
	public function actionFindByLocation() {
		$lat = $_GET['lat'];
		$lon = $_GET['lon'];
		$rad = $_GET['rad'];
		$sex = isset($_GET['sex']) ? $_GET['sex'] : null;
		$res = array();
		$models = User::model()->findByLocation($lat, $lon, $rad, $sex);
		foreach($models as $model) {
			$info = $model->getPublicInfo();
			$info['distance'] = $model->distance;
			$res[] = $info;
		}
		$this->_sendSuccess($res);
	}

	public function actionFindByCriteria() {
		$u = new User('search');
		$u->attributes = $_GET;
		$c=new CDbCriteria;
		$c->compare('age', $u->age);
		$c->compare('area', $u->area);
		$c->compare('purpose', $u->purpose);
		$c->compare('nickname', $u->nickname);
		$c->compare('profile_id', $u->profile_id);
		
		$res = array();
		$models = $u->findAll($c);
		foreach($models as $model) {
			$info = $model->getPublicInfo();
			$res[] = $info;
		}
		$this->_sendSuccess($res);
	}
	
	public function actionFind($s) {
		$res = array();
		$models = User::model()->findAll("profile_id LIKE :s OR nickname LIKE :s", array('s'=>"%$s%"));
		foreach($models as $model) {
			$info = $model->getPublicInfo();
			$res[] = $info;
		}
		$this->_sendSuccess($res);
	}

	public function actionGetVisitors($id) {
		$v = Visitor::model()->tableName();
		$c=new CDbCriteria;
		$c->select = 't.*, v1.created_time AS visit_time';
		$c->join = "
			JOIN $v v1 ON v1.visitor_id=t.id AND v1.user_id=:id
			LEFT JOIN $v v2 ON v2.visitor_id=v1.visitor_id 
				AND v2.user_id=v1.user_id 
				AND v1.created_time < v2.created_time 
		";
		$c->params = array('id'=>$id);
		$c->addCondition("v2.id IS NULL");
		$c->order = 'v1.created_time DESC';
		$res = array();
		$models = User::model()->findAll($c);
		foreach($models as $model) {
			$info = $model->getPublicInfo();
			$info['visit_time'] = $model->visit_time;
			$res[] = $info;
		}
		$this->_sendSuccess($res);
	}

	public function actionLogVisitor($id) {
		$v = new Visitor();
		$v->visitor_id = $_POST['visitor_id'];
		$v->user_id = $id;
		$v->created_time = date('Y-m-d H:i:s');
		$res = $v->save();
		if ($res) {
			$v->visitor->addPoint('view_prof', false);
		}
		$this->_sendSuccess($res);
	}

	public function actionTest() {
		$u = User::model()->findByPk(1);
		$u->addPoint('view_prof', false);
		// if ($inviter = $u->inviter) {
			// $inviter->addPoint('invite_recv');
		// }
	}
}